<template>
  <div class="xtx-city" ref="target">
    <div class="select" @click="toggle" :class="{ active: active }">
      <span class="value" v-if="userAddress">{{ userAddress }}</span>
      <span class="placeholder" v-else>请选择配送地址</span>
      <i class="iconfont icon-angle-down"></i>
    </div>
    <div class="option" v-show="active">
      <span
        class="ellipsis"
        v-for="item in cityList"
        :key="item.code"
        @click="selectCity(item)"
        >{{ item.name }}</span
      >
    </div>
  </div>
</template>

<script lang="ts" setup name="XtxCity">
// 城市列表类型
export type AreaList = {
  code: string
  level: number
  name: string
  areaList: AreaList[]
}
export type changeResult = {
  provinceCode: string
  provinceName: string
  cityCode: string
  cityName: string
  countyCode: string
  countyName: string
}
import { ref, watch } from 'vue'
import axios from 'axios'
import { onClickOutside } from '@vueuse/core'

defineProps<{
  userAddress?: string
}>()

const emit = defineEmits<{
  (e: 'changeCity', value: changeResult): void
}>()

const active = ref(false)
const toggle = () => {
  active.value = !active.value
}

// 监听元素外部的点击
const target = ref(null)
onClickOutside(target, () => {
  active.value = false
})

const cityList = ref<AreaList[]>([])
const cacheList = ref<AreaList[]>([])
// 获取城市列表
const getCityList = async () => {
  const res = await axios.get<AreaList[]>(
    'https://yjy-oss-files.oss-cn-zhangjiakou.aliyuncs.com/tuxian/area.json'
  )
  cityList.value = res.data
  // 缓存起来，关闭时重新赋值
  cacheList.value = res.data
}
getCityList()
// 选择城市
const changeResult = ref({
  provinceCode: '',
  provinceName: '',
  cityCode: '',
  cityName: '',
  countyCode: '',
  countyName: ''
})
// 省市区选择逻辑
const selectCity = (item: AreaList) => {
  // 省
  if (item.level === 0) {
    changeResult.value.provinceCode = item.code
    changeResult.value.provinceName = item.name
    cityList.value = item.areaList
  }
  // 市
  if (item.level === 1) {
    changeResult.value.cityCode = item.code
    changeResult.value.cityName = item.name
    cityList.value = item.areaList
  }
  // 区
  if (item.level === 2) {
    changeResult.value.countyCode = item.code
    changeResult.value.countyName = item.name
    active.value = false
    // 选择完省市区后传递给父组件
    emit('changeCity', changeResult.value)
  }
}

// 关闭后恢复城市数据
watch(active, (value) => {
  // 当关闭active的时候，需要回复数据
  if (!value) {
    cityList.value = cacheList.value
  }
})
</script>

<style scoped lang="less">
.xtx-city {
  display: inline-block;
  position: relative;
  z-index: 400;

  .select {
    border: 1px solid #e4e4e4;
    height: 30px;
    padding: 0 5px;
    line-height: 28px;
    cursor: pointer;

    &.active {
      background: #fff;
    }

    .placeholder {
      color: #999;
    }

    .value {
      color: #666;
      font-size: 12px;
    }

    i {
      font-size: 12px;
      margin-left: 5px;
    }
  }

  .option {
    width: 542px;
    border: 1px solid #e4e4e4;
    position: absolute;
    left: 0;
    top: 29px;
    background: #fff;
    min-height: 30px;
    line-height: 30px;
    display: flex;
    flex-wrap: wrap;
    padding: 10px;

    > span {
      width: 130px;
      text-align: center;
      cursor: pointer;
      border-radius: 4px;
      padding: 0 3px;

      &:hover {
        background: #f5f5f5;
      }
    }
  }
}
</style>
